package jfb.examples.gmf.filesystem.diagram.part.transform;

import java.util.List;

import jfb.examples.gmf.filesystem.diagram.part.DiagramEditorContextMenuProvider;
import jfb.examples.gmf.filesystem.diagram.part.FilesystemDiagramEditorPlugin;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gmf.runtime.common.ui.services.action.contributionitem.ContributionItemService;
import org.eclipse.gmf.runtime.diagram.ui.actions.ActionIds;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.providers.DiagramContextMenuProvider;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;

public class TransformationContextMenuProvider extends DiagramContextMenuProvider {
	private IWorkbenchPart mPart;
	private TransformationStorage mStorage;
	
	public static String TRANSFORMATION_FOLDER = "icons/";
	
	public TransformationContextMenuProvider(IWorkbenchPart part,
			EditPartViewer viewer) {
		super(part, viewer);
		mPart = part;
		mStorage = new TransformationStorage();
		mStorage.init(TRANSFORMATION_FOLDER + "transformations.txt");
		mStorage.initializeActions(part);
	}
	
	public void dispose() {
		super.dispose();
	}
	
	public void buildContextMenu(final IMenuManager menu) {
		getViewer().flush();
		FilesystemDiagramEditorPlugin.getInstance().logInfo("Selected object: " + getSelectedObject() + ". Class: " + getSelectedObject().eClass().getName());
	    try {
			TransactionUtil.getEditingDomain(
					(EObject) getViewer().getContents().getModel())
					.runExclusive(new Runnable() {

						public void run() {
							ContributionItemService
									.getInstance()
									.contributeToPopupMenu(
											TransformationContextMenuProvider.this,
											mPart);
							for (TransformationAction action : mStorage.getActionsForElement(getSelectedObject())) {
								menu.appendToGroup("editGroup", action);
							}
						}
					});
		} catch (Exception e) {
			FilesystemDiagramEditorPlugin.getInstance().logError(
					"Error building context menu", e);
		}
	}
	
	public EObject getSelectedObject() {
		ISelection selection = null;
        ISelectionService selectionService = null;
        if (mPart != null && mPart.getSite().getWorkbenchWindow() != null) {
            selectionService = mPart.getSite()
                .getWorkbenchWindow().getSelectionService();
        }

        if (selectionService != null) {
            selection = selectionService.getSelection();
        }
        IStructuredSelection sselection = (selection instanceof StructuredSelection) ? (StructuredSelection) selection
                : StructuredSelection.EMPTY;
        return (sselection.toList().size() > 1) ? null : ((View)((GraphicalEditPart)sselection.toList().get(0)).getModel()).getElement();
	}

}
